package com.itheima.controller;

import com.itheima.annotation.Log;
import com.itheima.pojo.entity.Clazz;
import com.itheima.pojo.dto.ClazzQuery;
import com.itheima.pojo.vo.PageResult;
import com.itheima.pojo.vo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    // 班级分页查询
    @GetMapping
    public Result page(ClazzQuery clazzQuery) {
        log.info("班级分页查询clazzQuery:{}", clazzQuery);
        PageResult<Clazz> pageResult = clazzService.page(clazzQuery);
        return Result.success(pageResult);
    }

    //根据id删除班级
    @DeleteMapping("/{id}")
    @Log
    public Result delete(@PathVariable Integer id) {
        log.info("根据id删除班级,id:{}", id);
        clazzService.delete(id);
        return Result.success();
    }
    //根据id查询班级
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("根据id查询班级,id:{}", id);
        Clazz clazz = clazzService.findById(id);
        return Result.success(clazz);
    }
    //修改班级
    @PutMapping
    @Log
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级，clazz:{}",clazz);
        clazzService.updateClazz(clazz);
        return Result.success();
    }

    //新增班级
    @PostMapping
    @Log
    public Result save(@RequestBody Clazz clazz){
        log.info("新增班级，clazz:{}",clazz);
        clazzService.saveClazz(clazz);
        return Result.success();
    }
    //查询所有班级
    @GetMapping("/list")
    public Result findAll(){
        log.info("查询所有班级。");
        List<Clazz> clazzList = clazzService.findAll();
        return Result.success(clazzList);
    }

}
